﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Automobile.service.Common;
using Automobile.service.Model;
using System.Data;
using System.Data.SqlClient; 
using System.Net;
namespace Automobile.service.DAL
{
   public class EmployeeDAL
    {
       SQLHelper heper = new SQLHelper();
       /// <summary>
       /// 显示员工信息
       /// </summary>
       /// <returns></returns>
       public List<Employee>Empoyee_Show()
       {
           string sql = "Select Employee_Id,Employee_Name,Employee_Pwd,Employee_Phone,Employee_State,Employee_Addr,Employee_Remark from dbo.Employee";
           DataTable table = heper.ExecDataTable(sql);
           List<Employee> list = new List<Employee>();
           foreach (DataRow item in table.Rows)
           {
               Employee m = new Employee();
               m.Employee_Id = Convert.ToInt32(item["Employee_Id"]);
               m.Employee_Name = item["Employee_Name"].ToString();
               m.Employee_Pwd = item["Employee_Pwd"].ToString();
               m.Employee_Phone = item["Employee_Phone"].ToString();
               m.Employee_Addr = item["Employee_Addr"].ToString();
               m.Employee_State = Convert.ToInt32(item["Employee_State"]);
               m.Employee_Remark = item["Employee_Remark"].ToString();
               list.Add(m);
           }
           return list;

       }
       /// <summary>
       /// 根据ID获取信息
       /// </summary>
       /// <param name="id"></param>
       /// <returns></returns>
       public DataTable Empoyee_Hu(int id)
       {
           string sql = "Select Employee_Id,Employee_Name,Employee_Pwd,Employee_Phone,Employee_Addr,Employee_State,Employee_Remark from dbo.Employee where Employee_Id=" + id + "";
           return heper.ExecDataTable(sql);
       }
       /// <summary>
       /// 修改
       /// </summary>
       /// <param name="e"></param>
       /// <returns></returns>
       public int Empoyee_Upt(Employee e)
       {
           string sql = "update Employee set Employee_Name='" + e.Employee_Name + "',Employee_Pwd='" + e.Employee_Pwd + "',Employee_Phone='" + e.Employee_Phone + "',Employee_State='" + e.Employee_State + "',Employee_Remark='" + e.Employee_Remark + "',Employee_Addr='" + e.Employee_Addr + "'  where Employee_Id=" + e.Employee_Id + "";
           return heper.ExecNonquery(sql);
       }
       /// <summary>
       /// 删除
       /// </summary>
       /// <param name="id"></param>
       /// <returns></returns>
       public int Empoyee_Del(int id)
       {
           string sql = "delete from Employee where Employee_Id=" + id + "";
           return heper.ExecNonquery(sql);
       }
       /// <summary>
       /// 添加
       /// </summary>
       /// <param name="e"></param>
       /// <returns></returns>
       public int Empoyee_Add(Employee e)
       {
           string sql = "insert into Employee(Employee_Name,Employee_Pwd,Employee_Phone,Employee_State,Employee_Remark,Employee_Addr) values('" + e.Employee_Name + "','" + e.Employee_Pwd + "','" + e.Employee_Phone + "','" + e.Employee_State + "','" + e.Employee_Remark + "','" + e.Employee_Addr + "')";
           return heper.ExecNonquery(sql);
       }

       /// <summary>
       /// 登录方法
       /// </summary>
       /// <param name="name"></param>
       /// <param name="pwd"></param>
       /// <returns></returns>
       public int login(string name, string pwd)
       {
           string AddressIP = string.Empty;
           foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
           {
               if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
               {
                   AddressIP = _IPAddress.ToString();
               }
           }
           string sql = "Journal_Show";
           SqlParameter[] Par = new SqlParameter[] { 
                         new SqlParameter("@Employee_Name",name),
                         new SqlParameter("@Employee_Pwd",pwd),
                         new SqlParameter("@Journa_Date",DateTime.Now),
                         new SqlParameter("@JournalIP",AddressIP),
           
           };
           return Convert.ToInt32(heper.ExecScalar_Proc(sql,Par));
       }
       public int count()
       {
           string sql = "select count(*) from Employee";
           return Convert.ToInt32(heper.ExecScalar(sql));
       }
       ///// <summary>
       ///// 添加数据
       ///// </summary>
       ///// <param name="j"></param>
       ///// <returns></returns>
       //public int add(Journal j)
       //{

       //    string sql = login(name)
       //    string str = "insert into Journal values('" + j.Journa_Date + "','" + j.Employee_Id + "','" + j.Journa_Content + "','" + j.JournalIP + "')";
       //    return H.ExecNonquery(str);
       //}
      
    }
}
